package com.v2future.sjt.collegecheckingattendance.fruit.research;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.v2future.sjt.collegecheckingattendance.MainActivity;
import com.v2future.sjt.collegecheckingattendance.R;
import com.v2future.sjt.collegecheckingattendance.fruit.bean.ConditionTypeBean;
import com.v2future.sjt.collegecheckingattendance.fruit.bean.ResearchTypeBean;
import com.v2future.sjt.collegecheckingattendance.utils.BaseResponseBean;
import com.v2future.sjt.collegecheckingattendance.utils.Constants;
import com.v2future.sjt.collegecheckingattendance.utils.IntentKey;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.BaseRequestParams;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetTaskListener;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetUtils;

import java.io.Serializable;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 科研类型列表
 *
 * @author shijianting
 * @time 2016/5/20 13:07
 */
public class ResearchTypeListActivity extends MainActivity {

    @Bind(R.id.lv_research_type)
    ListView lvResearchType;

    /**
     * 科研类别
     */
    private List<ResearchTypeBean> researchTypeBeanList;

    private ResearchTypeAdapter researchTypeAdapter;
    private ConditionTypeBean conditionTypeBean;

    @Override
    public void init() {
        conditionTypeBean = (ConditionTypeBean) getIntent().getSerializableExtra(IntentKey.FRUIT_TYPE);
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_research_type_list);
        ButterKnife.bind(this);
        initTitleBack(null);
        setTitleText(conditionTypeBean.getConditonTypeName());
    }

    @Override
    public void initAfter() {
        doGetType();
    }

    private void initResearchTypeView(){
        if (null == researchTypeBeanList || researchTypeBeanList.size() == 0) {
            tip("获取科研类型错误");
            return;
        }
        researchTypeAdapter = new ResearchTypeAdapter(researchTypeBeanList, this);
        lvResearchType.setAdapter(researchTypeAdapter);
        lvResearchType.setOnItemClickListener(typeClickListener);
    }

    /**
     * 获取科研类别
     */
    public void doGetType() {
        BaseRequestParams params = new BaseRequestParams(this, formatUrl(Constants.GET_RESEARCH_TYPE));
        NetUtils.doGetRequest(this, params, new NetTaskListener<ResearchTypeResponeBean>() {

            @Override
            public void onSuccess(ResearchTypeResponeBean result) {
                researchTypeBeanList = result.getData();
                initResearchTypeView();
            }

            @Override
            public void onError() {

            }

            @Override
            public void onInterError(ResearchTypeResponeBean result) {

            }

            @Override
            public void onNetError(int responseCode, String responseMsg, String errorResult) {

            }

            @Override
            public void onCancelled() {

            }

            @Override
            public void onFinished() {

            }

            @Override
            public void onSuccessCache(ResearchTypeResponeBean result) {

            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    /**
     * 类型回复
     */
    public class ResearchTypeResponeBean extends BaseResponseBean implements Serializable {
        List<ResearchTypeBean> data;

        public List<ResearchTypeBean> getData() {
            return data;
        }

        public void setData(List<ResearchTypeBean> data) {
            this.data = data;
        }
    }

    /**
     * 科研类型点击
     */
    AdapterView.OnItemClickListener typeClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // TODO: 2016/5/20 转入具体科研类型
            ResearchTypeBean bean = researchTypeBeanList.get(position);
            Intent intent = new Intent(ResearchTypeListActivity.this, OneResearchTypeListActivity.class);
            intent.putExtra(IntentKey.FRUIT_TYPE, conditionTypeBean);
            intent.putExtra(IntentKey.RESEARCH_TYPE, bean);
            startActivity(intent);
        }
    };
}
